//
//  MyCenterViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/11/6.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "MyCenterViewController.h"
#import "YJLoginViewController.h"
#import "ResultListViewController.h"
#import "MyClassRoomViewController.h"
#import "MyHomeworkViewController.h"
#import "SendFriendCircleViewController.h"
#import "ClassmateViewController.h"
#import "PopularityViewController.h"
#import "SignViewController.h"
#import "MonthDestinationViewController.h"
#import "MemorandumListViewController.h"
#import "MyEvaluationViewController.h"
#import "YJUserEditViewController.h"

@interface MyCenterViewController ()
@property(nonatomic,strong) UIImageView * headImageView;
@property(nonatomic,strong)UIView * tableHeaderView;

@property(nonatomic,strong)NSArray *imageArray;
@property(nonatomic,strong)NSArray *textArray;
@property(nonatomic,strong)NSArray *viewControllerArray;

@property(nonatomic,assign)BOOL isNotifications;
@end

@implementation MyCenterViewController

-(UIView *)tableHeaderView{
    if (!_tableHeaderView) {
        _tableHeaderView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,kScreenW, kScreenH / 5*2)];
        _tableHeaderView.userInteractionEnabled=YES;
    }
    return _tableHeaderView;
}

-(UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH / 5*2)];
        _headImageView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; //自动调整view的高度，以保证上边距和下边距不变,自动调整view的宽度，保证左边距和右边距不变
        _headImageView.clipsToBounds=YES;
        _headImageView.userInteractionEnabled=YES;
        _headImageView.contentMode=UIViewContentModeScaleAspectFill;
        _headImageView.backgroundColor = KBACKNAVIGATIONCOLOR;
    }
    return _headImageView;
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
//    [Global setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {
        //学生
        self.imageArray = @[@"MyCenter_Record",@"MyCenter_MyHomework",@"MyCenter_HistoryClass",@"MyCenter_CollectionClass",@"MyCenter_MyselfComment",@"MyCenter_Monthly",@"MyCenter_classmate"];
        self.textArray = @[@"成绩记录",@"我的作业",@"历史课程",@"收藏课程",@"我的自评",@"月目标月总结",@"我的同班同学"];
        self.viewControllerArray = @[@"ResultListViewController",@"MyHomeworkViewController",@"MyClassRoomViewController",@"MyClassRoomViewController",@"MyEvaluationViewController",@"MonthDestinationViewController",@"ClassmateViewController"];
    }else if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"2"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"3"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"4"]){
        //家长
        self.imageArray = @[@"MyCenter_CollectionClass",@"MyCenter_HistoryClass",@"pesentCenter_myChild",@"pesentCenter_investigation"];
        self.textArray = @[@"收藏的课程",@"历史课程",@"我的孩子",@"问卷调查"];
        self.viewControllerArray = @[@"MyClassRoomViewController",@"MyClassRoomViewController",@"ClassmateViewController",@"JSFeedbackViewController"];
    }else{
        //教师
        self.imageArray = @[@"MyCenter_CollectionClass",@"MyCenter_HistoryClass",@"teacher_memorandum",@"pesentCenter_school",@"pesentCenter_investigation"];//,@"搜索我的学生"(备忘录后面)
        self.textArray = @[@"收藏的课程",@"历史课程",@"备忘录",@"我的学校",@"问卷调查"];//,@"搜索我的学生"
        self.viewControllerArray = @[@"JSHistoryViewController",@"JSNewsViewController",@"MemorandumListViewController",@"MySchoolListViewController",@"JSFeedbackViewController",@""];
    }
    
    CGRect rect = self.view.bounds;
    rect.size.height -=  29;
    rect.origin.y -= 20;//44
    self.tableView.frame = rect;
    [self getCurrentUserInfo];
    [self creatHeadView];
}
- (void)getCurrentUserInfo{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"];   //token
    
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppApiGetCurrentUserInfo] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithDictionary:response];
            [JHUserInfo saveUserInfo:dic[@"userInfo"]];
        }
    }];
}
#pragma mark - creatHeadView
- (void)creatHeadView{
    [self.tableHeaderView addSubview:self.headImageView];
    self.tableView.tableHeaderView = self.tableHeaderView;
    
    self.headImageView.image = [UIImage imageNamed:@"MyCenter_bg"];
    UIImageView *iconImage = [[UIImageView alloc]init];
    iconImage.frame = CGRectMake( (kScreenW - 80)/2, kScreenH / 11, 80, 80);
    iconImage.clipsToBounds = YES;
    iconImage.layer.cornerRadius = 40;
    iconImage.layer.borderColor = [UIColor whiteColor].CGColor;
    iconImage.layer.borderWidth = 2;
    iconImage.userInteractionEnabled = YES;
    [iconImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editIcon:)]];
    MyLog(@"----------------------%@",[JHUserInfo getUserInfo].icon);
    if (!strIsEmpty([JHUserInfo getUserInfo].icon)) {
        [iconImage sd_setImageWithURL:[NSURL URLWithString:[JHUserInfo getUserInfo].icon] placeholderImage:KDEFAULTHEAD];
    }else{
        iconImage.image = KDEFAULTHEAD;
    }
    [self.tableHeaderView addSubview:iconImage];

    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.frame = CGRectMake(0, CGRectGetMaxY(iconImage.frame) + 5, kScreenW, 25);
    nameLabel.text = [JHUserInfo getUserInfo].nick_name;
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont systemFontOfSize:18];
    [self.tableHeaderView addSubview:nameLabel];

    UILabel *desLabel = [[UILabel alloc]init];
    desLabel.frame = CGRectMake(0, CGRectGetMaxY(nameLabel.frame) + 5, kScreenW, 25);
    desLabel.text = [NSString stringWithFormat:@"积分：%@",[JHUserInfo getUserInfo].score];//  人气值：%@
    desLabel.textColor = [UIColor whiteColor];
    desLabel.textAlignment = NSTextAlignmentCenter;
    desLabel.font = [UIFont systemFontOfSize:14];
    [self.tableHeaderView addSubview:desLabel];
    
    
    UIView *footView = [[UIView alloc]init];
    footView.frame = CGRectMake(0, 0, kScreenW, 80);
    self.tableView.tableFooterView = footView;
    
    CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(10, 15, kScreenW - 20, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"退出登录"];
    [completeButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:completeButton];
}
- (void)editIcon:(UIGestureRecognizer *)sender{
    YJUserEditViewController *userEditVC = [[YJUserEditViewController alloc]init];
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {
        userEditVC.userEditRole = KMyCenterEditStudent;
    }else if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"2"] ||[[JHUserInfo getUserInfo].role_type isEqualToString:@"3"] || [[JHUserInfo getUserInfo].role_type isEqualToString:@"4"]){
        userEditVC.userEditRole = KMyCenterEditPerson;
    }else{
        userEditVC.userEditRole = KMyCenterEditTeacher;
    }
    
    [self.navigationController pushViewController:userEditVC animated:YES];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {
        return 2;
    }
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"] && !section) {
        return 1;
    }
    return self.textArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == [self.tableView numberOfSections]-1) {
        static NSString *CellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            CustomeButton *cusButton = [[CustomeButton alloc]init];
            cusButton.relative = KRelativeAlignmentRight;
            cusButton.frame = CGRectMake(20, 7, 200, 30);
            cusButton.tag = 1000;
            [cusButton setTitleColor:KTEXTMAINCOLOR forState:UIControlStateNormal];
            [cell.contentView addSubview:cusButton];
        }
        CustomeButton *cusButton = [cell.contentView viewWithTag:1000];
        cusButton.imageRect = CGRectMake(0, 3, 24, 24);
        cusButton.titleRect = CGRectMake(CGRectGetMaxX(cusButton.imageRect) + 10, 5, 150, 20);
        cusButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        [cusButton setImage:[UIImage imageNamed:self.imageArray[indexPath.row]] forState:UIControlStateNormal];
        cusButton.userInteractionEnabled = NO;
        [cusButton setTitle:self.textArray[indexPath.row] forState:UIControlStateNormal];
        
        return cell;
    }else if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]){
        static NSString *CellIdentifier = @"last";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        NSArray *imgArray = @[@"MyCenter_Sign",@"MyCenter_MoodDiary",@"MyCenter_Future",@"MyCenter_Term goals"];
        NSArray *tilArray = @[@"心情签到",@"心情日记",@"未来畅想",@"学期目标"];
        for (int i = 0; i < imgArray.count; i ++) {
            CustomeButton *cusButton = [[CustomeButton alloc]init];
            cusButton.relative = KRelativeAlignmentDown;
            cusButton.frame = CGRectMake(i * (kScreenW / 4), 10, kScreenW/4, kScreenW/4);
            cusButton.imageRect = CGRectMake((kScreenW / 4 - 30)/2, 10, 30, 30);
            cusButton.titleRect = CGRectMake(0, CGRectGetMaxY(cusButton.imageRect) + 5, kScreenW / 4, 20);
            cusButton.titleLabel.textAlignment = NSTextAlignmentCenter;
            cusButton.titleLabel.font = [UIFont systemFontOfSize:14];
            [cusButton setTitleColor:KTEXTMAINCOLOR forState:UIControlStateNormal];
            [cusButton setImage:[UIImage imageNamed:imgArray[i]] forState:UIControlStateNormal];
            [cusButton setTitle:tilArray[i] forState:UIControlStateNormal];
            cusButton.tag = 9000 + i;
            [cusButton addTarget:self action:@selector(sendContent:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:cusButton];
        }
        
        return cell;
    }
    static NSString *CellIdentifier = @"cell0";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleDefault;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"] && !indexPath.section) {
        return 80;
    }
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"2"] ||[[JHUserInfo getUserInfo].role_type isEqualToString:@"3"] || [[JHUserInfo getUserInfo].role_type isEqualToString:@"4"]) {
        switch (indexPath.row) {
            case 0:{
                MyClassRoomViewController *classRoomVC = [[MyClassRoomViewController alloc]init];
                classRoomVC.classRoomType = KCollectionClassRoom;
                classRoomVC.titleStr = @"收藏课程";
                [self.navigationController pushViewController:classRoomVC animated:YES];
            }
                break;
            case 1:{
                MyClassRoomViewController *classRoomVC = [[MyClassRoomViewController alloc]init];
                classRoomVC.classRoomType = KHistoryClassRoom;
                classRoomVC.titleStr = @"历史课程";
                [self.navigationController pushViewController:classRoomVC animated:YES];
            }
                break;
            case 2:{
                ClassmateViewController *classmateVC =  [[ClassmateViewController alloc]init];
                classmateVC.personListType = KPersentPersonListType;
                [self.navigationController pushViewController:classmateVC animated:YES];
            }
                break;
            case 3:{
                WebViewController *webView = [[WebViewController alloc]initWithTitle:@"问卷调查" withRequest:@"http://wjdc.ziyushi.com/exam/exam.html?role=%E9%97%AE%E5%8D%B7"];
                [self.navigationController pushViewController:webView animated:YES];
            }
                break;
            default:{
                TableViewPresentViewController *vc =  [[NSClassFromString(self.viewControllerArray[indexPath.row]) alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
        }
    }else if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"5"] ||[[JHUserInfo getUserInfo].role_type isEqualToString:@"6"] || [[JHUserInfo getUserInfo].role_type isEqualToString:@"7"]) {
        switch (indexPath.row) {
            case 0:{
                    MyClassRoomViewController *classRoomVC = [[MyClassRoomViewController alloc]init];
                    classRoomVC.classRoomType = KCollectionClassRoom;
                    classRoomVC.titleStr = @"收藏课程";
                    [self.navigationController pushViewController:classRoomVC animated:YES];
            }
                break;
            case 1:{
                    MyClassRoomViewController *classRoomVC = [[MyClassRoomViewController alloc]init];
                    classRoomVC.classRoomType = KHistoryClassRoom;
                    classRoomVC.titleStr = @"历史课程";
                    [self.navigationController pushViewController:classRoomVC animated:YES];
            }
                break;
            case 2:{
                MemorandumListViewController *reaultListVC = [[MemorandumListViewController alloc]init];
                reaultListVC.memorandumListType = KMemorandumListType;
                [self.navigationController pushViewController:reaultListVC animated:YES];
            }
                 break;
            case 4:{
                WebViewController *webView = [[WebViewController alloc]initWithTitle:@"问卷调查" withRequest:@"http://wjdc.ziyushi.com/exam/exam.html?role=%E9%97%AE%E5%8D%B7"];
                [self.navigationController pushViewController:webView animated:YES];
            }
                break;
                
            default:{
                TableViewPresentViewController *vc =  [[NSClassFromString(self.viewControllerArray[indexPath.row]) alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
        }
    }else{
        if (indexPath.row == 0) {
            ResultListViewController *resultListVC = [[ResultListViewController alloc]init];
            resultListVC.monthListType = KResultMonthListType;
            [self.navigationController pushViewController:resultListVC animated:YES];
        }else if (indexPath.row == 1){
            PopularityViewController *popularityVC = [[PopularityViewController alloc]init];
            popularityVC.titleStr = @"我的作业";
            popularityVC.titleArray = @[@"课堂作业",@"课后作业"];
            popularityVC.vcStr = @"MyHomeworkViewController";
            [self.navigationController pushViewController:popularityVC animated:YES];
        }else if (indexPath.row == 2){
            MyClassRoomViewController *classRoomVC = [[MyClassRoomViewController alloc]init];
            classRoomVC.classRoomType = KHistoryClassRoom;
            classRoomVC.titleStr = @"历史课程";
            [self.navigationController pushViewController:classRoomVC animated:YES];
        }else if (indexPath.row == 3){
            MyClassRoomViewController *classRoomVC = [[MyClassRoomViewController alloc]init];
            classRoomVC.classRoomType = KCollectionClassRoom;
            classRoomVC.titleStr = @"收藏课程";
            [self.navigationController pushViewController:classRoomVC animated:YES];
        }else if (indexPath.row == 4){
            MemorandumListViewController *reaultListVC = [[MemorandumListViewController alloc]init];
            reaultListVC.memorandumListType = KMyEvaluationType;
            [self.navigationController pushViewController:reaultListVC animated:YES];
        }else if (indexPath.row == 6){
            ClassmateViewController *classmateVC =  [[ClassmateViewController alloc]init];
            classmateVC.personListType = KStudentPersonListType;
            [self.navigationController pushViewController:classmateVC animated:YES];
        }else{
            TableViewPresentViewController *vc =  [[NSClassFromString(self.viewControllerArray[indexPath.row]) alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
   
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 0.01);
    view.backgroundColor = KBACKGROUNDCOLOR;
    if (![[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {
        view.frame = CGRectMake(0, 0, kScreenW, 10.01);
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (![[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {
        return 10.01f;
    }
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 10);
    view.backgroundColor = KBACKGROUNDCOLOR;
    if (section == self.tableView.numberOfSections - 1) {
        view.frame = CGRectMake(0, 0, kScreenW, 0.01);
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == self.tableView.numberOfSections-1) {
        return 0.01f;
    }
    return 10.01f;
}
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    CGPoint offset = scrollView.contentOffset;
//    if (offset.y < 0) {
//        CGRect rect =self.tableHeaderView.frame;
//        rect.origin.y = offset.y;
//        rect.size.height =CGRectGetHeight(rect)-offset.y;
//        self.headImageView.frame = rect;
//        self.tableHeaderView.clipsToBounds=NO;
//    }
//}


#pragma mark - 发心情日志，未来畅想，学期目标
- (void)sendContent:(UIButton *)sender{
    switch (sender.tag) {
        case 9000:{
            SignViewController *signVC = [[SignViewController alloc]init];
            [self.navigationController pushViewController:signVC animated:YES];
        }
            break;
        case 9001:{
            MemorandumListViewController *reaultListVC = [[MemorandumListViewController alloc]init];
            reaultListVC.memorandumListType = KMoodDiaryListType;
            [self.navigationController pushViewController:reaultListVC animated:YES];
            
        }
            break;
        case 9002:{
            MemorandumListViewController *reaultListVC = [[MemorandumListViewController alloc]init];
            reaultListVC.memorandumListType = KFutureMyselfListType;
            [self.navigationController pushViewController:reaultListVC animated:YES];
        }
            break;
        case 9003:{
            MemorandumListViewController *reaultListVC = [[MemorandumListViewController alloc]init];
            reaultListVC.memorandumListType = KTermGoalsListType;
            [self.navigationController pushViewController:reaultListVC animated:YES];
        }
            break;
        default:
            break;
    }
}
#pragma mark - 退出登录
- (void)backLogin{
    //退出
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"确定退出登录吗?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        return;
    }];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [Global setUserTokenInfo:@""]; //把token置空
        [JHUserInfo removeUserInfo];//删除保存的个人信息
        [[Global appdelegete] enterTheMainViewIndex:0];
    }];
    [alert addAction:cancel];
    [alert addAction:sure];
    [self presentViewController:alert animated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
